package com.cyw.note.manage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.cyw.common.utils.StringUtils;
import com.cyw.common.core.page.TableDataInfo;
import com.cyw.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.cyw.note.manage.domain.bo.MyprojectNoteBo;
import com.cyw.note.manage.domain.vo.MyprojectNoteVo;
import com.cyw.note.manage.domain.MyprojectNote;
import com.cyw.note.manage.mapper.MyprojectNoteMapper;
import com.cyw.note.manage.service.IMyprojectNoteService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 文章管理Service业务层处理
 *
 * @author myproject
 * @date 2022-08-06
 */
@RequiredArgsConstructor
@Service
public class MyprojectNoteServiceImpl implements IMyprojectNoteService {

    private final MyprojectNoteMapper baseMapper;

    /**
     * 查询文章管理
     */
    @Override
    public MyprojectNoteVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询文章管理列表
     */
    @Override
    public TableDataInfo<MyprojectNoteVo> queryPageList(MyprojectNoteBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MyprojectNote> lqw = buildQueryWrapper(bo);
        Page<MyprojectNoteVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询文章管理列表
     */
    @Override
    public List<MyprojectNoteVo> queryList(MyprojectNoteBo bo) {
        LambdaQueryWrapper<MyprojectNote> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MyprojectNote> buildQueryWrapper(MyprojectNoteBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MyprojectNote> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), MyprojectNote::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), MyprojectNote::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), MyprojectNote::getDescription, bo.getDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), MyprojectNote::getType, bo.getType());
        return lqw;
    }

    /**
     * 新增文章管理
     */
    @Override
    public Boolean insertByBo(MyprojectNoteBo bo) {
        MyprojectNote add = BeanUtil.toBean(bo, MyprojectNote.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改文章管理
     */
    @Override
    public Boolean updateByBo(MyprojectNoteBo bo) {
        MyprojectNote update = BeanUtil.toBean(bo, MyprojectNote.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MyprojectNote entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除文章管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
